package com.product.system.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.EasyExcel;
//import com.product.system.config.MinioConfig;
import com.product.system.domain.MenuBarChart;
import com.product.system.domain.SysMenu;
import com.product.system.domain.response.SysShowForm;
import com.product.system.service.ISysMenuService;
import com.product.system.utils.Result;
//import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class SysMenuController {
    @Autowired
    private ISysMenuService menuService;

    /**
     * 菜单列表
     * @return
     */
    @RequestMapping("/list")
    public Result list(){

        List<SysMenu> nodeList = menuService.selectMenu();
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();

        //转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
         List<Tree<String>> treeNodes = TreeUtil.<SysMenu, String>build(nodeList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getMenuId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setName(treeNode.getMenuName());
                    // 扩展属性 ...
                    tree.putExtra("path", treeNode.getPath());
                    tree.putExtra("component",treeNode.getComponent());
                    tree.putExtra("icon",treeNode.getIcon());
                });
        return Result.success(treeNodes);
    }

//    @RequestMapping("/menuList")
//    public Result menuList(Integer current,Integer size){
//        PageHelper.startPage(current,size);
//        List<SysMenu> list = menuService.selectMenu();
//        PageInfo<SysMenu> pageInfo = new PageInfo<>(list);
//        return Result.success(pageInfo);
//    }

    @RequestMapping("/treeselect")
    public Result treeselect(Integer menuId){
        SysMenu menu = menuService.treeselect(menuId);
        return Result.success(menu);
    }

    /**
     * 菜单列表
     * @param menu
     * @return
     */
    @RequestMapping("/list1")
    public Result list1(@RequestBody SysMenu menu){
        System.out.println(menu.getStatus()+""+menu.getMenuName());
        List<SysMenu> nodeList = menuService.selectMenu1(menu);
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();

        //转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<SysMenu, String>build(nodeList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getMenuId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setName(treeNode.getMenuName());
                    // 扩展属性 ...
                    tree.putExtra("path", treeNode.getPath());
                    tree.putExtra("component",treeNode.getComponent());
                    tree.putExtra("icon",treeNode.getIcon());
                    tree.putExtra("orderNum",treeNode.getOrderNum());
                    tree.putExtra("status",treeNode.getStatus());
                    tree.putExtra("createTime",DateUtil.format(treeNode.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
                    tree.putExtra("pic",treeNode.getPic());
                });
        return Result.success(treeNodes);
    }


    /**
     * 菜单列表
     * @param
     * @return
     */
    @RequestMapping("/list2")
    public Result list2(){
        List<SysMenu> nodeList = menuService.selectMenu3();
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();

        //转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<SysMenu, String>build(nodeList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getMenuId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setName(treeNode.getMenuName());
                    // 扩展属性 ...
                    tree.putExtra("path", treeNode.getPath());
                });
        return Result.success(treeNodes);
    }

    /**
     * 导出Excel菜单信息
     * @param response
     * @return
     * @throws IOException
     */
    @GetMapping("/easyExcel")
    public Result easyExcel(HttpServletResponse response) throws IOException {
        String fileName = URLEncoder.encode("菜单信息", "UTF-8").replaceAll("\\+", "%20");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
        List<SysMenu> list = menuService.selectMenu();
        EasyExcel.write(response.getOutputStream(), SysMenu.class).sheet("菜单信息").doWrite(list);
        return Result.success("导出成功");
    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @RequestMapping("/addMenu")
    public Result addMenu(@RequestBody SysMenu menu){

        System.err.println(menu.getPic()+"===========================");

        Boolean b = menuService.addMenu(menu);
        return b ? Result.success("添加成功") : Result.fail("添加失败");
    }


    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @RequestMapping("/deleteMenuById")
    public Result deleteMenuById(Long menuId){
        Boolean b = menuService.deleteMenuById(menuId);
        return b ? Result.success("删除成功") : Result.fail("删除失败");
    }

    /**
     * 修改菜单
     * @param menu
     * @return
     */
    @RequestMapping("/updateMenu")
    public Result updateMenu(@RequestBody SysMenu menu){
        Boolean b = menuService.updateMenu(menu);
        return b ? Result.success("修改成功") : Result.fail("修改失败");
    }

//    @Autowired
//    private MinioClient minioClient;


    /**
     * 上传图片
     * @param
     * @return
     * @throws IOException
     */
//    @RequestMapping("/upload")
//    public String upload(MultipartFile file) throws IOException {
//        String name = file.getOriginalFilename();
//        name = IdUtil.getSnowflakeNextIdStr()+"_"+ name;
//        try {
//            minioClient.putObject(PutObjectArgs.builder()
//                    .bucket("2111a")
//                    .object(name)
//                    .stream(file.getInputStream(), file.getSize(), -1)
//                    .contentType(file.getContentType())
//                    .build());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        String url = " http://127.0.0.1:9000/"+"2111a"+"/"+name;
//        System.out.println(url);
//        System.out.println(name);
//        return url;
//    }

    @RequestMapping("/showMenu")
    public Result showMenu() {
        List<MenuBarChart> list = menuService.showMenu();
        return Result.success(list);
    }



















}
